var ws, aid, spage, wtype, dataList;
var totalPrice = 0.00, nums = 0.00;
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	aid = ws.aid;//病例Id
//	spage = ws.spage;
	wtype = ws.wtype;
	console.log(aid+" "+wtype);
	showWating();
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=medicinal&a=Mfield",
		dataType :'json',
		success : function(data){
			//味性
			var comedies = data.comedies.split(',');
			var htm1 = '<li class="mui-table-view-cell coms" data-id=00><a href="#top-1">全部</a></li>';
			$.each(comedies, function(ke, vo) {
				plus.nativeUI.closeWaiting();
				come = vo.split('=');
				htm1 += '<li class="mui-table-view-cell coms" data-id="'+ come[1] +'"><a href="#top-1">'+come[0]+'</a></li>';
			});
			$('.comedies').html(htm1);
			$(".coms").on('tap',function(){
				showWating();
				$(".mui-col-xs-4:eq(0) span:eq(0)").text($(this).text());
				$(".mui-col-xs-4:eq(1) span:eq(0)").text('功效');
				$(".mui-col-xs-4:eq(2) span:eq(0)").text('三品');
				$(".mui-col-xs-4:eq(0) span:eq(0)").attr('data-id',$(this).attr('data-id'));
				console.log($(this).attr('data-id'));
				seacrchM($(this).attr('data-id'),'comedies');
			});	
			//功效
			var effect = data.effect.split(',');
			var htm2 = '<li class="mui-table-view-cell effs" data-id=00><a href="#top-2">全部</a></li>';
			$.each(effect, function(k, v) {
				effe = v.split('=');
				htm2 += '<li class="mui-table-view-cell effs" data-id="'+ effe[1] +'"><a href="#top-2">'+effe[0]+'</a></li>';
			});
			$('.effect').html(htm2);
			$(".effs").on('tap',function(){
				showWating();
				$(".mui-col-xs-4:eq(0) span:eq(0)").text('味性');
				$(".mui-col-xs-4:eq(1) span:eq(0)").text($(this).text());
				$(".mui-col-xs-4:eq(2) span:eq(0)").text('三品');
				$(".mui-col-xs-4:eq(1) span:eq(0)").attr('data-id',$(this).attr('data-id'));
				seacrchM($(this).attr('data-id'),'effect');
			});
			//三品
			var attributes = data.attributes.split(',');
			var htm3 = '<li class="mui-table-view-cell atts" data-id=00><a href="#top-3">全部</a></li>';
			$.each(attributes, function(key, value) {
				attris = value.split('=');
				htm3 += '<li class="mui-table-view-cell atts" data-id="'+ attris[1] +'"><a href="#top-3">'+attris[0]+'</a></li>';
			});
			$('.attributes').html(htm3);
			$(".atts").on('tap',function(){
				showWating();
				$(".mui-col-xs-4:eq(0) span:eq(0)").text('味性');
				$(".mui-col-xs-4:eq(1) span:eq(0)").text('功效');
				$(".mui-col-xs-4:eq(2) span:eq(0)").text($(this).text());
				$(".mui-col-xs-4:eq(2) span:eq(0)").attr('data-id',$(this).attr('data-id'));
				seacrchM($(this).attr('data-id'),'attributes');
			});
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
	//药物列表
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=medicinal&a=selectM",
		dataType : 'json',
		success :function (data){
//			console.log(JSON.stringify(data));   
			dataList = data;
			var list = '';
			$.each(data, function(k,v) { 
				plus.nativeUI.closeWaiting();
				list += '<div class="mui-table-view medicine-lists overflow-h mui-table-view-cell" data-id="'+v.id+'">'+
							'<div><img src="'+webRoot+v.litpic+'" alt="" /></div>'+
							'<div class="overflow-h">'+
								'<li class="font-16">'+v.title+'</li>'+
								'<li class="color-red font-13">'+v.price+'元/克</li>'+
								'<li>'+ 
									'<span class="mui-pull-right">'+
										'<div class="mui-numbox fr edit_goods" style="z-index: 100;" data-numbox-min="0" data-numbox-max="100">'+
											'<button class="mui-btn mui-numbox-btn-minus" type="button" pic="' + v.litpic + '" pr="' + v.price + '" title="' + v.title + '">-</button>'+
											'<input class="mui-numbox-input" type="number" data-id ="'+v.id+'" value=0 disabled/>'+
											'<button class="mui-btn mui-numbox-btn-plus" type="button" pic="' + v.litpic + '" pr="' + v.price + '" title="' + v.title + '">+</button>'+
										'</div>'+
//										'<button class="background-y medicine-add-btn">添加</button>'+
									'</span>'+
								'</li>'+
							'</div>'+
						'</div>';
			});
			$('.medicine').html(list);
			//按钮加减事件
			clickEvent();
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
	//搜索药物
	$('.search').on('input paste', function() {
			var search = $('.search').val();
			console.log(search);
			if(!search.trim()){
				$('.search').val('');
				toast('搜索内容不能为空');
				return;
			}
			if(dataList.length > 0){
				$('.medicine .medicine-lists').each(function(k, v) {
					var _this = $(this);
					var name = _this.find('li[class=font-16]').text();
					console.log(name);
					if(name.indexOf(search) >= 0){
						_this.show();
					}else{
						_this.hide();
					}
				})
			}
	})
	var zongji;
	$('.vice').click(function(){
		zongji = $('.zongji').text(); 
		if(zongji == '0元' || zongji == '0.00元'){
			$(this).val('');
			$(this).blur();
			toast('您还未选择药材');return;
		}
		console.log(zongji);
	})
	//几副药
	$('.vice').bind('input propertychange', function() {
		console.log($(this).val());
		if(zongji && $(this).val() > 0){
			var a = parseFloat(zongji)*parseFloat($(this).val());
			$('.zongji').text(parseFloat(a).toFixed(2) +"元");
		}
	})
	//查看详情
	$('.details').click(function(){
		//获取添加的药材信息
		var zongji = $('.zongji').text();
		var arrId = '';
		var num, nums = '';
		$('input[type=number]').each(function(index){
			num = $(this).val();
			if(num > 0){
				arrId += $(this).attr('data-id') + ",";
				nums += num + ',';
			}
		})
		var vice = $('.vice').val();
		if(arrId==',' || nums==',' || !arrId || !nums){
			toast('您还未选择药材');return;
		}
		if(!vice || parseFloat(vice) > 20){
			toast('药材数量有误');return;
		}
		console.log(arrId +'   ' + nums + '   ' +zongji+ '   ' +vice);
		goNewPage('free-medicine-list-2.html', {arrId:arrId,nums:nums,zongji:zongji,aid:aid,spage:ws.id,wtype:wtype,vice:vice});
	})
},false)
var clickEvent=function(){
	//mui +- 数量 失效 重写
	$(".edit_goods button ").on('tap',function(){
		var bThis=$(this).parent();
		var bmin=parseInt(bThis.attr('data-numbox-min'));//设置最小值
		var bmax=parseInt(bThis.attr('data-numbox-max'));//设置最大值
		var binput=parseInt(bThis.find("input").val());//当前数量
		if($(this).text()=='+'){
			binput++
			bThis.find("input").val(binput>bmax?bmax:binput);
			//获取数量变化时合计的价格
			nums++;
			var price = $(this).attr('pr');
			var vice = $('.vice').val();
//			console.log(vice);
			if(vice){//已经选好几副药，修改药材剂量
				var zongji = $('.zongji').html();
				totalPrice = parseFloat(zongji) + (parseFloat(price) * parseFloat(vice));
			}else{
				totalPrice += parseFloat(price);
			}
			$('.zongji').html(totalPrice.toFixed(2) + '元');
		}else{
			binput--
			bThis.find("input").val((binput<bmin)?bmin:binput);
			//获取数量变化时合计的价格
			if (binput != -1) {
				if (nums >= 0) {
					nums--;
					var price = $(this).attr('pr');
					var vice = $('.vice').val();
//					console.log(vice);
					if(vice){//已经选好几副药，修改药材剂量
						var zongji = $('.zongji').html();
						totalPrice = parseFloat(zongji) - (parseFloat(price) * parseFloat(vice));
					}else{
						totalPrice -= parseFloat(price);
					}
					if(totalPrice.toFixed(2) == '0.00'){
						$('.vice').val('');
					}
					$('.zongji').html(totalPrice.toFixed(2) + '元');
					
				}
			}else{
				$('.vice').val('');
			}
		}
	})
}
//搜索的结果——>a:选中的值，b:属于哪个选框
function seacrchM(a,b){
	console.log(apiRoot+"?m=home&c=medicinal&a=selectM&field="+a+"&type="+b);
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=medicinal&a=selectM&field="+a+"&type="+b,
		dataType :'json',
		success :function(data){
//			console.log(JSON.stringify(data));
			if(data.length > 0){
				var list = '';
				$.each(data, function(k,v) {
					plus.nativeUI.closeWaiting();
					list += '<div class="mui-table-view medicine-lists overflow-h mui-table-view-cell" data-id="'+v.id+'">'+
								'<div><img src="'+webRoot+v.litpic+'" alt="" /></div>'+
								'<div class="overflow-h">'+
									'<li class="font-16">'+v.title+'</li>'+
									'<li class="color-red font-13">'+v.price+'元/克</li>'+
									'<li>'+ 
										'<span class="mui-pull-right">'+
											'<div class="mui-numbox fr edit_goods" data-numbox-min="0" data-numbox-max="100">'+
												'<button class="mui-btn mui-numbox-btn-minus" type="button" pic="' + v.litpic + '" pr="' + v.price + '" title="' + v.title + '">-</button>'+
												'<input class="mui-numbox-input" type="number" data-id ="'+v.id+'" value=0 disabled/>'+
												'<button class="mui-btn mui-numbox-btn-plus" type="button" pic="' + v.litpic + '" pr="' + v.price + '" title="' + v.title + '">+</button>'+
											'</div>'+
	//										'<button class="background-y medicine-add-btn">添加</button>'+
										'</span>'+
									'</li>'+
								'</div>'+
							'</div>';
				});
				$('.medicine').html(list);
				//按钮加减事件
				clickEvent();
			}else{
				plus.nativeUI.closeWaiting();
				$('.medicine').html('');
				toast('无相关药品');
			}
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}
